/**
 * Created by kitabi on 2017/6/20.
 */
/**
 * Created by kitabi on 2017/6/16.
 */
var classify={

};

var $btnStaffConfirm= $("#btnStaffConfirm");
var $staffDialog= $("#staffDialog");
/**
 * 获取用户数据
 */
classify.queryList= function(){


    $("#mainTable").bootstrapTable({

        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        striped: true,                              //是否显示行间隔色
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        url:"/system/qryJoinList",
        // height: $(window).height() - 110,
        // width:$(window).width(),
        // showColumns:true,
        pagination:true,
        // queryParams : queryParams,
        minimumCountColumns:2,
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 10,                       //每页的记录行数（*）
        pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
        uniqueId: "id",                     //每一行的唯一标识，一般为主键列
        showExport: true,
        exportDataType: 'all',


        paginationPreText:"<",
        paginationNextText:">",
        queryParams: function(params) {


            return {
                cPage: (params.offset/params.limit)+1,
                pSize: params.limit,
                city:$("#city").val(),
                province:$("#province").val(),
                area:$("#area").val()
            };
        },
        columns: [
            {
                field: '',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            },{
                field: 'province',
                title: '省份'
            },{
                field: 'city',
                title: '城市'
            },{
                field: 'area',
                title: '区县'
            },{
                field: 'cast_num',
                title: '加盟费',
                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    debugger;
                    var cast_num= row.cast_num;
                    var id= row.id;
                    var text= '<input  onkeypress="keyPress()" type="text" id="cast_num_'+id+'" value="'+cast_num+'"/>';
                    return  text;
                }
            }, {

                field: 'status',
                title: '状态',
                align: 'center',

                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var status= row.status;
                    var name="";
                    if(status ==0){
                        name="未加盟";
                    }else if(status ==1){
                        name="已加盟";
                    }
                    return  name;
                }
            }, {

                field: 'status',
                title: '操作',
                align: 'center',

                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var id= row.id;
                    var status= row.status;

                    var add='<button type="button" onclick="updateStatus('+id+')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">保存</button>';

                    var e='<button type="button" onclick="showDetail('+index+',\'update\')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">编辑</button>';
                    return  add+e;
                }
            }]

    });

}

function updateStatus(id ){
    var cast_num=$("#cast_num_"+id).val();
    if(cast_num == ""){
        alert("请输入加盟费");
        return;
    }
    var param={
        id: id,
        cast_num:cast_num
    }
    Ajax.ajax({
        url: "/system/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function keyPress() {
    var keyCode = event.keyCode;
    if ((keyCode >= 48 && keyCode <= 57))
    {
        event.returnValue = true;
    } else {
        event.returnValue = false;
    }
}


function allpush(status){

    var select= $("#mainTable").bootstrapTable('getSelections');

    if(select.length ==0){
        alert("请先勾选一行");
        return;
    }

    var idlist= "";
    for(var i=0;i<select.length;i++){
        var temp = select[i];
        if(status ==3){
            if(temp.status != 2){

                alert("请注意，提现单号: "+temp.id+"  已发货或还未支付！");
                return ;
            }
        }

        if(status ==7){
            if(temp.status != 6 ){

                alert("请注意，提现单号: "+temp.id+"  不能退款！");
                return ;
            }
        }

        if(i==0){
            idlist+=temp.id;
        }else {
            idlist += "," + temp.id;
        }
    }

    var param={
        id: idlist,
        status: status

    }
    Ajax.ajax({
        url: "/order/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function addType() {
    var brandName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(brandName == ""){
        alert("请输入品牌名称");
        return;
    }
    var param = {
        brandName:brandName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/add",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function clearDialog() {
    $("#Sname").val("");
    $("#indexs").attr("data-id","");
    $("#indexs").val("");
    $("#userSearch").val();
    $staffDialog.hide();
}

function showDetail(index,type){

    if(type == "add"){

        $btnStaffConfirm.off('click');
        $btnStaffConfirm.on('click',addType);
    }else if(type == "update"){
        var data= $("#mainTable").bootstrapTable("getData")[index];//获取当前列的数据


        $("#indexs").attr('data-id',data.id);
        $("#indexs").val(data.indexs);
        $("#Sname").val(data.type_name);
        $btnStaffConfirm.off('click');
        $btnStaffConfirm.on('click',updateType);
    }

    $staffDialog.show();

}
function updateType() {
    var id = $("#indexs").attr('data-id');
    var offerTypeName = $("#Sname").val();
    var indexs= $("#indexs").val();
    if(offerTypeName == ""){
        alert("请输入名称");
        return;
    }
    var param = {
        id:id,
        offerTypeName:offerTypeName,
        indexs:indexs
    }
    Ajax.ajax({
        url: "/offertype/update",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                alert(res.data);
                clearDialog();
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert(res.data);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function queryProvince(){
    var param = {

    }
    Ajax.ajax({
        url: "/common/qryProvinces",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option value='"+res[i].provinceid+"'>"+res[i].province+"</option>");
                $("#province").append(option);
            }

        }
    });
}

function queryCitys(){
    $("#city").html("");
    if($("#province").val()==""){
        $("#city").val("");

        $("#area").val("");
        return;
    }

    var param = {
        province:$("#province").val()
    }
    Ajax.ajax({
        url: "/common/qryCities",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option value='"+res[i].cityid+"'>"+res[i].city+"</option>");
                $("#city").append(option);
            }


        }
    });
}

function qryAreas(){
    $("#area").html("");
    if($("#city").val()==""){

        $("#area").val("");

        return;
    }
    var param = {
        city:$("#city").val()
    }
    Ajax.ajax({
        url: "/common/qryAreas",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            for(var i=0;i<res.length;i++){
                var option=$("<option value='"+res[i].areaid+"'>"+res[i].area+"</option>");
                $("#area").append(option);
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

$(function(){



    classify.queryList();

    queryProvince();

    $("#userSearchBtn").click(function(){
        $("#mainTable").bootstrapTable('refresh');
    });
});